package com.zsj.juc.jmm;

import java.util.concurrent.TimeUnit;

/**
 * @author zsj
 * @version 1.0
 * @date 2024/5/6 21:10
 * @description
 */
public class VolatileAtomicDemo {
    public static void main(String[] args) {
        Number number = new Number();
        for (int i = 0; i < 10; i++) {
            new Thread(() -> {
                for (int j = 0; j < 1000; j++) {
                    number.add();
                }
            }, "" + i).start();
        }

        //保证上面执行完成
        try {
            TimeUnit.MILLISECONDS.sleep(2000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        System.out.println(number.number);
    }

}


//资源类
class Number {
    int number;

    public void add() {
        number++;
    }
}